如何使用GraphQL Relay连接修复循环依赖关系

时间:2017-06-13 16:40:09

标签: javascript graphql relayjs relay graphql-js

我已将我与该文件的连接分开并可以在我的根查询中使用它(当在类型中定义连接时它工作正常),但是当尝试通过将其导入到类型时使用它连接是因为我回来了:

  

错误:PageEdge.node字段类型必须是输出类型,但得到:undefined

我可以在QueryType中使用连接,但不能使用PageType。我的架构大致是这样的。

QueryType {
  pages: [PageType]
}

PageType {
  _id: string
  pages: [PageType]
}

这是一个指向项目仓库的链接,其中有graphql的东西(我只是推送了一个提交,错误地看到所有代码): https://github.com/DaveyEdwards/myiworlds/tree/master/src/data

我已经使QueryType和PageType字段成为thunk(这个解决方案似乎解决了大多数人的问题),并尝试用接口创建一个thunk:

interfaces: () => [nodeInterface]

对于任何与用户和朋友构建应用的人来说,我认为这是一个非常常见的问题。

UserType: {
  friends: [UserType]
}

我的PageType:

import { nodeInterface } from '../nodeInterface';
import PageConnection from './connections/PageConnection';

const PageType = new ObjectType({
  name: 'Page',
  description: 'Everything you see can be placed inside a page.',
  fields: () => ({
    id: globalIdField('Page', page => page._id),
    _id: {
      type: new NonNull(ID),
      description: 'A unique id used to instantly locate this page inside the database',
    },
    pageEdge: {
      type: PageConnection,
      args: connectionArgs,
      resolve: async (page, { args }, { loaders }) => {
        if (page.pageEdge) {
          const pageEdge = await loaders.pageLoader.loadMany(page.pageEdge);
          const connection = connectionFromArray(pageEdge, args);
          return connection;
        }
      },
    },
  }),
  interfaces: () => [nodeInterface],
});

export default PageType;

我的PageConnection:

import { connectionDefinitions } from 'graphql-relay';

import PageType from '../PageType';

const { connectionType: PageConnection } = connectionDefinitions({
  name: 'Page',
  nodeType: PageType,
});

export default PageConnection;

我的nodeInterface:

import { nodeDefinitions } from 'graphql-relay';
import { getNode, getNodeType } from './type-registry';

export const { nodeInterface, nodeField } = nodeDefinitions(getNode, getNodeType);

我的类型注册表:

import { fromGlobalId } from 'graphql-relay';
require('babel-polyfill');

const types = {};

export const registerType = (model, type, lookupFn) => {
  types[type.name] = { model, type, lookupFn };
};

export const getNode = async (globalId) => {
  const { type: typeName, id } = fromGlobalId(globalId);
  console.log('getNode', globalId, typeName, id);

  if (types[typeName]) {
    const object1 = await types[typeName].lookupFn(id);
    const Class = types[typeName].model;
    // let result  = Object.create(types[typeName].model, object1);
    const result = new Class(object1);
    console.log('getNode result', result);
    return result;
  }
  return null;
};

export const getNodeType = (obj) => {
  const keys = Object.keys(types);
  let ret = null;
  keys.map((typeName) => {
    if (obj instanceof types[typeName].model) {
      ret = types[typeName].type;
    }
    return true;
  });
  return ret;
};

导入的连接工作的My QueryType

import {
  GraphQLObjectType as ObjectType,
  GraphQLList as List,
  GraphQLString as StringType,
} from 'graphql';
import { connectionArgs, connectionFromArray, connectionDefinitions } from 'graphql-relay';
import { nodeField } from '../nodeInterface';
import PageType from './PageType';
import { getPageList } from '../queries/googleDatastore/pageQueries';
import PageConnection from './connections/PageConnection';

const QueryType = new ObjectType({
  name: 'QueryType',
  fields: () => ({
    pages: {
      type: PageConnection,
      args: connectionArgs,
      resolve: async (obj, args) => {
        const response = [];
        try {
          const pageEdge = await getPageList();
          const connection = connectionFromArray(pageEdge, args);
          return connection;
        } catch (err) {
          console.log('pages err', err);
        }
        return response;
      },
    },
    node: nodeField,
  }),
});

export default QueryType;

此问题的资源我一直在努力工作:

这个仓库的一部分是我为实现连接工作而实施的 https://github.com/bondz/learn-graphql-relay/blob/47211fdec44ce4bb10f487bddfc7411e5690b894/src/types/projectConnection.js

Edge.node field type must be Output Type but got: undefined

https://github.com/graphql/graphql-js/issues/612

https://github.com/graphql/graphql-js/issues/373

https://gist.github.com/fbaiodias/77406c29ddf37fe46c3c

1 个答案:

答案 0 :(得分:7)

只需删除import PageConnection顶部的PageType.js并使用运行时要求:

...
pageEdge: {
  type: require('./connections/PageConnection'),
  args: connectionArgs,
...