Невозможно использовать определение типа конструктора в определении типа строки - PullRequest
0 голосов
/ 21 октября 2019

Я пытаюсь объединить определение типа конструктора и определение типа строки. Вот моя попытка:

Я использую определение типа строки с gql для user модуля

modules/user/typeDefs.ts:

import { gql } from 'apollo-server';

export const typeDefs = gql`
  type User {
    id: ID!
    name: String
    email: String

    # Use constructor type 'postType'
    posts: [Post]!
  }

  type Query {
    user(id: ID!): User
  }
`;

modules/user/schema.ts:

import { resolvers } from './resolvers';
import { typeDefs } from './typeDefs';
import { makeExecutableSchema } from 'apollo-server';

export const userSchema = makeExecutableSchema({ typeDefs, resolvers });

Я использую определение типа конструктора для post модуля

modules/post/schema.ts:

import { GraphQLObjectType, GraphQLID, GraphQLNonNull, GraphQLString, GraphQLSchema } from 'graphql';
import { IAppContext } from '../../context';

const postType = new GraphQLObjectType({
  name: 'Post',
  fields: {
    id: { type: new GraphQLNonNull(GraphQLID) },
    title: { type: new GraphQLNonNull(GraphQLString) },
    authorId: { type: new GraphQLNonNull(GraphQLID) },
  },
});

const queryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    post: {
      type: postType,
      args: {
        id: { type: new GraphQLNonNull(GraphQLID) },
      },
      resolve: (_, { id }, { db }: IAppContext) => {
        return db.posts.find((post) => post.id.toString() === id);
      },
    },
  },
});

export const postSchema = new GraphQLSchema({ query: queryType });

Теперь я объединяю userSchema и postSchema вместе, используя mergeSchemas функция:

import { mergeSchemas } from 'apollo-server';

import { postSchema } from './modules/post/schema';
import { userSchema } from './modules/user/schema';

export const schema = mergeSchemas({ schemas: [postSchema, userSchema] });

Когда я пытаюсь запустить свой веб-сервер Apollo GraphQL, я получаю этот журнал:

☁  apollo-graphql-tutorial [master] ⚡  npx ts-node /Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/src/merge-constuctor-types-and-string-types/server.ts
Unknown type "Post".

Это даже не ошибка. Если я уберу поле posts из type User, оно будет работать нормально.

Как я могу решить эту проблему? Можно ли использовать определение типа конструктора в определении типа строки?

версии зависимостей:

"graphql": "^14.5.4",
"apollo-server": "^2.9.3",

Минимальный репозиторий для воспроизведения этой проблемы: https://github.com/mrdulin/apollo-graphql-tutorial/tree/master/src/merge-constuctor-types-and-string-types

...