The GraphQL docs give an example of a resolver function that accepts an argument called "context".
They have to say this about it -
context
A value which is provided to every resolver and holds important contextual information like the currently logged in user, or access to a database.
And their code example looks like this -
Query: {
human(obj, args, context) {
return context.db.loadHumanByID(args.id).then(
userData => new Human(userData)
)
}
}
This seems to me a perfectly natural pattern to want database access inside a resolver function, and unsurprisingly it is what I need to do.
This database context is not set up automatically, obviously, since GraphQL is completely agnostic about your particular means of data persistence.
My question is, how does one configure this context to provide one's specific db interface? I can't find mention of this in the tutorials/docs, or anywhere really.
Your GraphQL API can interact with any combination of data sources. Apollo provides a DataSource class that we can extend to handle interaction logic for a particular type of data source. In this section, we'll extend DataSource to connect both a REST API and a SQL database to Apollo Server.
A resolver is a function that resolves a value for a type or field in a schema. Resolvers can return objects or scalars like Strings, Numbers, Booleans, etc. If an Object is returned, execution continues to the next child field. If a scalar is returned (typically at a leaf node), execution completes.
Resolver is a collection of functions that generate response for a GraphQL query. In simple terms, a resolver acts as a GraphQL query handler. Every resolver function in a GraphQL schema accepts four positional arguments as given below −
Every resolver function in a GraphQL schema accepts four positional arguments as given below − //resolver function with no parameters and returning string greeting: () => { return "hello from TutorialsPoint !!!"
BE AWARE: that all arguments (source, args, context, info) are passed inside one argument called as resolveParams ( rp for brevity in the code). description public description which will be passed to graphql schema and will be available via introspection deprecationReason if you want to hide field from schema, but leave it working for old clients
A GraphiQL browser launches at localhost:5001 in which you can query your database. Try this query: Feel free to try other queries using the GraphQL Builder tab in GraphiQL. Go to the Code Exporter tab in the GraphiQL browser (under the Beta tab).
You may pass the context to graphql when calling the graphql
function.
It is specified here.
Here is the flow definition of the graphql
function:
graphql(
schema: GraphQLSchema,
requestString: string,
rootValue?: ?any,
contextValue?: ?any, // Arbitrary context
variableValues?: ?{[key: string]: any},
operationName?: ?string
): Promise<GraphQLResult>
context is defined when you set up your server. I couldn't see it in the docs either.
graphqlExpress(req => {
return {
schema: makeExecutableSchema({
typeDefs: schema.ast,
resolvers,
logger
}),
context: {
db: mongodb.MongoClient.connect(...)
}
};
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With