将内容类型导入流星中的schema.graphql

时间:2018-08-07 10:15:06

标签: reactjs meteor graphql apollo react-apollo

我很难过,试图将.graphql定义/内容类型导入到Meteor中的另一个.graphql文件中。 我尝试了诸如Meteor-GraphQl和著名的GraphQl-Import之类的软件包,但这些似乎都不起作用。 在我的应用中,我有一个api文件夹,其中包含我的所有定义:

-api
--Animal
---animal.graphql 
--Dog
---dog.graphql
---resolvers.js
---dog.js
---dogForm.js

在animal.graphql中,我具有以下内容:

enum A {
  hunted
  hunter
}
scalar Date

interface Animal {
  _id: String!
  name: String
  group: A!
  createdAt: Date
}

在dog.graphql中,我具有以下内容:

#import Animal "../Animals/Animal.graphql"

type Dog implements Animal{
  _id: String!
  name: String!
  group: A!
  ayes: Int!
  createdAt: Date
}

type Query {
  dog: [Dog]
}

type Mutation {
  createDog(
    name: String!
    group: A!
    ayes: Int!
    createdAt: Date
  ): Dog
}

如果我在dogForm.js中导入并记录了狗模式:

import DogSchema from "../Dog/Dog.graphql";
console.log("Dog Schema: ", DogSchema);

我发现未导入Animal模式,但出现错误:

Type "Animal" not found in document.

Here是一个codesandbox示例。 那么,在.grahql文件中导入内容类型和类型定义的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

这应该有效:

meteor add swydo:graphql

dog.graphql中:

#import "../Animal/animal.graphql"

type Dog implements Animal {
...

答案 1 :(得分:0)

我通过简单地导入SDL文件并在需要时将它们串联来解决了我的问题。实际上,如果已经编译Animal模式(makeExecutableSchema),则可以使用type Dog implements Animal{ ... }而不导入Animal #import Animal "../Animals/Animal.graphql"。因此,我最终使用globpathfs编写了一个函数,该函数会自动导入我的所有.graphql文件和解析器并使它们可执行。而且,当使用多个SDL时,我只需import它们并连接该字符串即可。

import AnimalSchema "../Animal/animal.graphql";
import DogSchema from "../Dog/Dog.graphql";
let contatSchema = AnimalSchema + DogSchema;