在ObjectType中,connectiondefinitions resolvenode vs resolve

时间:2016-12-16 07:23:43

标签: reactjs graphql relay

我正在使用graphql-relay,我在connectionDefinitions中看到,我可以为resolveNode提供一个函数,该函数被赋值给node节点对象中的resolve属性。但是在nodeType的定义中还有一个resolve属性。例如,我有一个带有resolveNode函数的Attendee connectionDefinition。我在我的meetType中使用该连接,它也具有解析功能。两个解析函数之间的区别是什么?

/**
 * Returns a GraphQLObjectType for a connection with the given name,
 * and whose nodes are of the specified type.
 */
export function connectionDefinitions(
  config: ConnectionConfig
): GraphQLConnectionDefinitions {
  const {nodeType} = config;
  const name = config.name || nodeType.name;
  const edgeFields = config.edgeFields || {};
  const connectionFields = config.connectionFields || {};
  const resolveNode = config.resolveNode;
  const resolveCursor = config.resolveCursor;
  const edgeType = new GraphQLObjectType({
    name: name + 'Edge',
    description: 'An edge in a connection.',
    fields: () => ({
      node: {
        type: nodeType,
        resolve: resolveNode,
        description: 'The item at the end of the edge',
      },
      cursor: {
        type: new GraphQLNonNull(GraphQLString),
        resolve: resolveCursor,
        description: 'A cursor for use in pagination'
      },
      ...(resolveMaybeThunk(edgeFields): any)
    }),
  });

  const connectionType = new GraphQLObjectType({
    name: name + 'Connection',
    description: 'A connection to a list of items.',
    fields: () => ({
      pageInfo: {
        type: new GraphQLNonNull(pageInfoType),
        description: 'Information to aid in pagination.'
      },
      edges: {
        type: new GraphQLList(edgeType),
        description: 'A list of edges.'
      },
      ...(resolveMaybeThunk(connectionFields): any)
    }),
  });

  return {edgeType, connectionType};
}

const {connectionType: attendeeConnection, edgeType: attendeeEdge} = connectionDefinitions({name: 'Attendee', nodeType: attendeeType, resolveNode:(root, args) => connectionFromPromisedArray(Attendee.getAttendees(root.attendees, args))});

const meetType = new GraphQLObjectType({
  name: 'Meet',
  description: 'A meeting',
  fields: () => ({
    id: globalIdField('Meet'),
    title: {
      type: GraphQLString
    },
    description: {
      type: GraphQLString
    },
    address: {
      type: addressType,
      description: 'Location of meet.',
      resolve: (root) => root.address
    },
    dateTime: {
      type: new GraphQLList(GraphQLDateTime)
    },
    messages: {
      type: messageConnection,
      description: 'List of messages.',
      args: connectionArgs,
      resolve: (root, args) => connectionFromPromisedArray(Message.getMessages(root._id), args),
    },
    todos: {
      type: todoConnection,
      description: 'List of tasks.',
      args: connectionArgs,
      resolve: (root, args) => connectionFromPromisedArray(Todo.getTodos(root._id), args),
    },
    attendees: {
      type: attendeeConnection,
      description: 'List of attendees.',
      args: connectionArgs,
      resolve: (root, args) => connectionFromPromisedArray(Attendee.getAttendees(root.attendees, args), args),
    }
  }),
  interfaces: [nodeInterface]
});

0 个答案:

没有答案