info argument is empty in Apollo GraphQL resolver type signature

*爱你&永不变心* 提交于 2019-12-11 06:33:41

问题


I'm working on this library https://github.com/ilyaskarim/wertik-js called Wertik JS to make GraphQL + Rest API more easily, In resolvers, when I console log info, it shows undefined. For each module, I have created dynamic resolvers to make things more easy for developers who will use this library.

let object = {
    create: async (_:any, args:any, context:any,info: any) => {
      console.log(info); // This will be undefined
      let v = await validate(validations.create,args.input);
      let {success} = v;
      if (!success) {
        throw new ApolloError("Validation error",statusCodes.BAD_REQUEST.number,{list: v.errors})
      }
      try {
        let createModel = await model.create(args.input);
        pubsub.publish(`${camelCase(moduleName)}Created`, { [`${camelCase(moduleName)}Created`]: createModel });
        return createModel;
      } catch (e) {
        return internalServerError(e);
      }
    },
}

Line: https://github.com/ilyaskarim/wertik-js/blob/ec813f49a14ddd6a04680b261ae4ef2aadc2b1a5/src/framework/dynamic/resolvers.ts#L102

The info is described in Apollo Server Documentation https://www.apollographql.com/docs/apollo-server/essentials/data/#resolver-type-signature, Which says: This argument contains information about the execution state of the query, including the field name, the path to the field from the root, and more. For me, unfortunately, it is getting undefined.

To reproduce the issue:

  1. Download https://github.com/ilyaskarim/wertik-js/tree/development
  2. Yarn install
  3. Go to examples/demo
  4. Run node index.js
  5. Now go to http://localhost:1209/
  6. Enter this mutation for example:

    mutation { createRole(input: {name: "Asd"}) { name } }

  7. This line executes on this mutation https://github.com/ilyaskarim/wertik-js/blob/ec813f49a14ddd6a04680b261ae4ef2aadc2b1a5/src/framework/dynamic/resolvers.ts#L102
  8. And returns undefined on the console.

This is how I setup the application:

const { ApolloServer } = require('apollo-server');

import mutations from "./loadAllMutations";
import queries from "./loadAllQueries";
import resolvers from "./loadAllResolvers";
import subscriptions from "./loadAllSubscriptions";
import schemas from "./loadAllSchemas";
import generalSchema from "./../helpers/generalSchema";

export default function (rootDirectory: string,app: any,configuration: object) {
  let allMutations = mutations(rootDirectory);
  let allQueries=  queries(rootDirectory);
  let allSchemas = schemas(rootDirectory);
  let allResolvers = resolvers(rootDirectory);
  let allSubscriptions = subscriptions(rootDirectory);
  let {validateAccessToken} = require(`${rootDirectory}/framework/predefinedModules/user/auth`).default;
  let mainSchema  = `
    ${generalSchema}
    ${allSchemas}
    type Subscription {
      ${allSubscriptions}
    }
    type Mutation {
      ${allMutations}
    }
    type Query {
      ${allQueries}
    }
    schema {
      query: Query
      mutation: Mutation
      subscription: Subscription
    }
  `;
  const server = new ApolloServer({ 
    typeDefs: mainSchema, 
    resolvers: allResolvers,
    context: async (a: any) => {
      await validateAccessToken(a.req);
    }
  });
  server.listen(1209).then(({ url, subscriptionsUrl }) => {
    console.log(`Server ready at ${url}`);
    console.log(`Subscriptions ready at ${subscriptionsUrl}`);
  });
}

What could be a possible reason?


回答1:


You're truncating the parameters received by the resolvers inside this module. If you need to assign a function to some object property, it's much better to just do it like this:

mutations: {
  [`create${moduleName}`]: mutations[`create${moduleName}`],
},

This is not only more succinct, but it also means you don't risk accidentally leaving off a parameter, which is what happened here.



来源:https://stackoverflow.com/questions/57136618/info-argument-is-empty-in-apollo-graphql-resolver-type-signature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!