Closed dschinkel closed 7 years ago
You should take id
argument in your queries:
# = GET /companies
companies: [Company!]!
# = GET /companies/:id
company(id: Int!): Company
Instead of a hard-coded id: 1
in your resolver:
# = GET /company
company: Company
So then you can get it from second argument args
:
resolve(parentValue, args, ctx) {
return Companies.find({ id: args.id });
}
Or using async resolve function:
async resolve(parentValue, args, ctx) {
const company = await Companies.find({ id: args.id });
// .....
return company;
}
If company id is not enough to construct your db queries, feel free to use fourth argument info
which is a GraphQLResolveInfo
. (fieldName, fieldNodes are included)
type GraphQLFieldResolveFn = (
source?: any,
args?: {[argName: string]: any},
context?: any,
info?: GraphQLResolveInfo
) => any
type GraphQLResolveInfo = {
fieldName: string,
fieldNodes: Array<Field>,
returnType: GraphQLOutputType,
parentType: GraphQLCompositeType,
schema: GraphQLSchema,
fragments: { [fragmentName: string]: FragmentDefinition },
rootValue: any,
operation: OperationDefinition,
variableValues: { [variableName: string]: any },
}
hard-coded id: 1
that was just for testing at first (mock data) and it's commented out
args.id
yea I didn't realize graphql handled the simple case where you aren't really filtering and just getting all records and certain fields. And...I thought returning all rows and all fields just to get all companies and only two specific fields would be inefficient but mySQL caches that stuff
using async resolve function
for me this will probably be just calling my controller's find() or whatever. I don't see a need to create a separate layer of "resolvers". My controller is basically a resolver, it's got a find() function that can do a lot of stuff like filtering, etc.
I wanted all rows so I won't need args yet but I understand what it's for once I do start to pass args to my fields. Thanks.
I am stuck with how to work with the incoming query. I want to be able to pass the requested fields from the graphql query and pass them to my controller which ultimately down the line sends those to my mySQL find() function which does a select id, name from Company.
I need to be able to send
id
,name
(or whatever the fields from the graph is that is requested) which I get from the graphql query that's incoming so that my find() can dynamically return requested fields per the graphql query.I wrote this test:
and it's using one of your helpers I found in your tests (I renamed it to queryString):
When I run this test, I see the following being passed to superagent:
On the koa service side, using koa-graphql, I have it setup like this:
or am I going about this the wrong way?