简体   繁体   English

Apollo GraphQL解析器类型签名中的info参数为空

[英]info argument is empty in Apollo GraphQL resolver type signature

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. 我正在使用这个名为Wertik JS的库https://github.com/ilyaskarim/wertik-js来简化GraphQL + Rest API,在解析器中,当我控制台日志info ,它显示未定义。 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 行: 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. 该信息在Apollo服务器文档https://www.apollographql.com/docs/apollo-server/essentials/data/#resolver-type-signature中进行了描述,其中说:该参数包含有关查询执行状态的信息,包括字段名称,从根到字段的路径等等。 For me, unfortunately, it is getting undefined. 对我来说,不幸的是,它变得不确定。

To reproduce the issue: 重现此问题:

  1. Download https://github.com/ilyaskarim/wertik-js/tree/development 下载https://github.com/ilyaskarim/wertik-js/tree/development
  2. Yarn install 纱线安装
  3. Go to examples/demo 转到示例/演示
  4. Run node index.js 运行node index.js
  5. Now go to http://localhost:1209/ 现在转到http:// localhost:1209 /
  6. Enter this mutation for example: 输入此突变,例如:

    mutation { createRole(input: {name: "Asd"}) { name } } 变异{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 此行在此突变上执行https://github.com/ilyaskarim/wertik-js/blob/ec813f49a14ddd6a04680b261ae4ef2aadc2b1a5/src/framework/dynamic/resolvers.ts#L102
  8. And returns undefined on the console. 并在控制台上返回undefined。

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? 可能是什么原因?

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. 这不仅更简洁,而且还意味着您不必冒意外遗漏参数的风险,这就是这里发生的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM