上下文未传递到嵌套的Apollo GraphQL解析器

时间:2019-06-24 17:38:30

标签: javascript graphql apollo apollo-server express-graphql

我正在为项目使用Apollo Server v2

我已经添加了https://www.apollographql.com/docs/apollo-server/features/authentication/

所示的身份验证

我想在我的QueryMutation中加入嵌套的解析器,所以我按照https://stackoverflow.com/a/40916089/7584077

问题是我的解析器比上面显示的解析器复杂一点

// typeDefs/typeDefs.js
import { gql } from "apollo-server-express";
import issueTracker from "./issueTracker";

const base = gql`
  scalar Timestamp

  type Query {
    ping: String!
  }

  type Mutation {
    ping: String!
  }
`;

export default [base, issueTracker];
// typeDefs/issuetracker.js
import { gql } from "apollo-server-express";

export default gql`
  type Comment {
    id: ID
    message: String
    commentBy: User
    createdAt: Timestamp
    updatedAt: Timestamp
    version: Int
  }

  type Issue {
    id: ID
    requestId: ID
    title: String
    issueNumber: Int
    status: Int
    tags: [String]
    assignees: [User]
    createdBy: User
    comments: [Comment]
    createdAt: Timestamp
    updatedAt: Timestamp
    version: Int
  }

  input CreateIssueRequest {
    requestId: ID!
    title: String!
    createdBy: ID!
    message: String!
    assignees: [ID]!
  }

  type IssueTrackerQuery {
    ping: String!
  }

  type IssueTrackerMutation {
    createIssue(request: CreateIssueRequest!): Issue
  }

  extend type Query {
    IssueTracker: IssueTrackerQuery
  }

  extend type Mutation {
    IssueTracker: IssueTrackerMutation
  }
`;

以及上述stackoverflow答案的lil修改版本。 这是我的组合解析器。

// resolvers/resolvers.js
import IssueTracker from "./issueTracker";

export default {
  Query: {
    ping: () => "ping!",
    IssueTracker: () => ({
      ping: IssueTracker.ping,
    }),
  },
  Mutation: {
    ping: () => "ping!",
    IssueTracker: () => ({
      createIssue: IssueTracker.createIssue,
    }),
  },
};

这是因为我希望 Query Mutation 完全分开。

这是我的IssueTracker解析器

// resolvers/issueTracker.js
export default {
  ping: () => "ping",
  createIssue: async (parent, args, context) => {
    console.log(parent);
    console.log(args);
    console.log(context);
    // create issue as per request and return
  }

这里的事情是,parent实际上是args字段! 而且我需要上下文中的userId来生成敏感数据。

1 个答案:

答案 0 :(得分:0)

嗯,SDL优先方法可能有点棘手。在这里很难解释什么地方出了问题,但我会尽力而为。首先,让我告诉您进行这项工作需要做什么,然后再解释出什么问题了。

在解析器映射中创建一个IssueTrackerMutation字段:

export default {
  Query: {
    ping: () => "ping!",
    IssueTracker: () => ({ // same here but I will just do the mutation for you
      ping: IssueTracker.ping,
    }),
  },
  Mutation: {
    ping: () => "ping!",
    IssueTracker: () => null, // or whatever you want as a root here
  },
  IssueTrackerMutation: {
    createIssue: IssueTracker.createIssue
  }
};

请注意,为IssueTracker创建“纯”解析器与使用createIssue方法为IssueTracker返回对象之间的区别。

现在应使用所需的参数调用该函数。父参数似乎丢失的原因是默认解析器的非常特殊的实现。解析程序旨在使用面向对象的样式,其中字段可以是字段方法。您可以想象解析器可以像这样工作:

defaultResolver(fieldName, parent, args, context, info) {
  if (typeof parent !== 'object') {
    throw "Need object to default resolve";
  }
  if (typeof parent[fieldName] === 'function') {
    return parent[fieldName](args, context, info);
  }
  return parent[fieldName];
}

这将允许您以以下方式编写数据访问对象:

class IssueTracker {
  issues = []
  async createIssue(args, ctx, info) {
    const issue = await createIssue(args.request);
    this.issues.push(issue);
    return issue;
  }
}

const issueTracker = new IssueTracker();

export default {
  // ...
  Mutation: {
    // ...
    IssueTracker: () => issueTracker,
  },
  IssueTrackerMutation: {
    createIssue: IssueTracker.createIssue
  }
};

讨论的还不多,但可能与Facebook的GraphQL的工作方式相对接近。社区似乎将更多逻辑转移到了解析器中。