graphql-community / koa-graphql

Create a GraphQL HTTP server with Koa.
MIT License
843 stars 61 forks source link

Working with query in resolve() #105

Closed dschinkel closed 7 years ago

dschinkel commented 7 years ago

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:

it.only('returns company data', async () => {
    const stub = {
        data: {
          Company: {
            id: 1,
            name: 'Pivotal'
          }
        }
      },
      query = queryString({
        query: `{
          company { 
            id,
            name
          }
        }`
      }),
      url = `/graphql?${query}`,
      response = await request
        .get(url),
      reponseData = JSON.parse(response.text);

    expect(reponseData).to.deep.equal(stub);
  });

and it's using one of your helpers I found in your tests (I renamed it to queryString):

function queryString(urlParams?: ?{ [param: string]: mixed }) {
  let querystring
  if (urlParams) {
    querystring = stringify(urlParams);
  }
  return querystring;
}

When I run this test, I see the following being passed to superagent:

screen shot 2017-09-09 at 12 42 06 pm

On the koa service side, using koa-graphql, I have it setup like this:

import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLID,
  GraphQLString
} from 'graphql';

import Companies from '../controllers/Company';

const Company = new GraphQLObjectType({
    name: 'Company',
    fields: () => ({
      id: {
        type: GraphQLID
      },
      name: {
        type: GraphQLString
      }
    })
  }),

  Query = new GraphQLObjectType({
    name: 'Query',
    fields: () => ({
      company: {
        type: Company,
        resolve(parentValue, args, ctx) {
          //not quite sure what to do here.  I need filter to represent an array of requested fields (in this case it's id and name)  from the graphql query or something like that
          const company = async () => Companies.find(filter);
          return company;
        }
      }
    })
  }),

/*  resolve() {
  return {
    id: 1,
    name: 'Pivotal'
  };
}*/

  mySqlSchema = new GraphQLSchema({
    query: Query
  });

export default mySqlSchema;

or am I going about this the wrong way?

chentsulin commented 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 },
}
dschinkel commented 7 years ago

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.