Hi everyone!..i am new to graphQL…I am using graphene library of python for GraphQL and added filter in query to fetch data as per a string present in an attribute but it is throwing null in output. Although if i am not using any filter then i got the output correctly.
So do i need to import any other package also to work with filters ??
Can you paste some code ?
yes, here is the code and query part:
import graphene
import json
import elasticsearch_dsl
DATA = [
{
"candidate_name" : "Jack",
"candidate_email" : "Jack@gmail.com",
"candidate_skills" : "machine learning, Artificial Intelligence"
},
{
"candidate_name" : "John",
"candidate_email" : "John@gmail.com",
"candidate_skills" : "python, machine learning"
}
class candidate(graphene.ObjectType):
candidate_name = graphene.String()
candidate_email = graphene.String()
candidate_skills = graphene.String()
class Query(graphene.ObjectType):
candidateInfo = graphene.List(candidate)
def resolve_candidateInfo(root, info):
return DATA
schema= graphene.Schema(query=Query)
print(schema)
query_graphql = '''
query myquery{
candidateInfo(filter:{candidate:{term:"python"}}){
candidateName
candidateEmail
.....
}
}
Please check if it helps!
And output i am getting null with this query:
{
query: Query
}
type Query {
candidateInfo: [candidate]
}
type candidate {
candidateName: String
candidateEmail: String
candidateSkills: String
}
null
Also from here in searching and filtering section: Getting Started with Python and GraphQL - Part 1 | Moesif Blog
i tried with search also, that too not working!
I assume your graphql schema on Dgraph is the same as this:
type candidate {
candidateName: String
candidateEmail: String
candidateSkills: String
}
?
I’m trying to replicate your problem atm… will update this with more questions
yes…please let me know where i am wrong in my code!
Do you have a schema in Dgraph? Or are you solely using Graphene’s schema?
using graphene only right now
OK you gotta have the same exact schema in your Dgraph instance. Are you using Dgraph Cloud or Dgraph on your local machine
To use ‘filtering’ in graphene…do i need to import any thing else or any changes needed in resolver? Looking forward to work on Dgraph schema also but before that trying with graphene an not getting output!
Please let me know if you found the problem in this.
Let’s take a step back and talk about what you are trying to accomplish. What is your goal in using graphene?
First let’s define what Graphene is:
Graphene is an opinionated Python library for building GraphQL schemas/types fast and easily.
- Easy to use: Graphene helps you use GraphQL in Python without effort.
- Relay: Graphene has builtin support for Relay.
- Data agnostic: Graphene supports any kind of data source: SQL (Django, SQLAlchemy), NoSQL, custom Python objects, etc. We believe that by providing a complete API you could plug Graphene anywhere your data lives and make your data available through GraphQL.
Now, let’s define what Graphene is not:
Graphene is not a GraphQL client.
I believe you want to use Dgraph’s GraphQL (as per the tag of this topic), and I assume, query and mutate data in a python project. If that is the case, then the python project is in essence the client to Dgraph and not a GraphQL server… And here is where the #1 difference is with Dgraph compared to EVERY OTHER GraphQL implementation: Dgraph provides a GraphQL API endpoint from the core WITHOUT another layer on top of Dgraph. Every other database needs a layer on top of the database to serve GraphQL using the data of the database (SQL, Mongo, DynamoDB, Neo4j, …). But with Dgraph, you already have GraphQL being served from the core.
Unless you are really wanting to build a python GraphQL layer on top of Dgraph’s endpoints (one being a GraphQL endpoint), then you probably don’t need graphene, but would be better off using something like gql.
In the off chance, that you really are trying to setup a python GraphQL server for another layer between Dgraph and your clients, then graphene may be the correct option. But… When you learn about GraphQL from Dgraph, the content is geared towards Dgraph’s GraphQL endpoint. With Dgraph, you place directives in the schema and then Dgraph generates payload types, inputs, queries, mutations, and even subscriptions if directed. But with most every other GraphQL server when you are creating a schema, you must manually by hand define all of the payload types, inputs, queries, mutations, and subscriptions that your server will need.
didn’t understood fully!..i simply want to know how could i use filtering/searching in my queries by using graphene.
^ This is not a Dgraph problem. If you want support for Graphene then ask over on their channels:
The below readme is the documentation for the
dev
(prerelease) version of Graphene. To view the documentation for the latest stable Graphene version go to the v2 docs
I don’t mean for that to sound harsh, but if you are trying to spin up a python GraphQL server, then don’t look at Dgraph’s GraphQL docs. You will need to do everything long form.
Did you define any inputs? Did you define the inputs for your query? I don’t see any inputs defined in your schema code pasted in the previous posts above.
See the docs for Graphene for an example where they defined an input: Graphene-Python
Can you answer this question: