如何将现有的GraphQLSchema对象传递给Apollo makeExecutableSchema函数

时间:2018-05-17 19:18:11

标签: graphql graphql-js

如何将graphql-iso-date之类的现有GraphQLSchema对象传递给makeExecutableSchema函数,并将其与字符串定义的类型和解析器函数一起使用?在下面的类型定义中说我希望date属性来自提到的包中的GraphQLDate

import { GraphQLDate, GraphQLTime, GraphQLDateTime } from 'graphql-iso-date';

let typeDefs = [];
typeDefs.push(`
  type MyType {
    date: Date
  }
`);

let resolvers = {
  Query: () => { /* ... */ },
};

makeExecutableSchema({ typeDefs, resolvers });

1 个答案:

答案 0 :(得分:0)

结果是resolvers地图,传递给makeExecutableSchema确实接受GraphQLScalarType而日期类型是标量。我们仍然需要手动将类型添加到typeDefs中......

typeDefs.push('scalar Date');

resolvers.Date = GraphQLDate;

所以我在我的项目中创建了一个外部标量模块并且正在做

import externalTypes from './externalTypes';
import printType from 'graphql';

// Define my typeDefs and resolvers here

for (let externalType of externalTypes) {
  let { name } = externalType;
  typeDefs.push(printType(externalType));
  resolvers[name] = externalType;
}

makeExecutableSchema({ typeDefs, resolvers });

我通过试用/失败来判断它,然后才在docs找到它,从而发布。此外,我仍然不知道如何以这种方式添加非标量类型(除了手动编写它的类型定义之外)。

同样,printType函数从传递的类型对象打印模式定义,在这里变得很方便(更多细节见this question)。