首页 文章

GraphQL架构测试:从架构到对象类型

提问于
浏览
0

如果特定的Field在我的ObjectType上,我想测试我的GraphQLSchema非常基本 .

我有以下TypeDefs .

export const categoryTypeDefs = `
  type Category {
    _id: ID!
    name: String!
  } ... `

该测试目前看起来像这样 .

describe('Category Schema', () => {
        const categorySchema = graphql.buildSchema(categoryTypeDefs)
        it('Should have an Category field of type String', () => {
            expect(categorySchema.getTypeMap).to.have.property("name");
        })
    })

现在我的问题是,是否有可能从我的Schema中获取Category的ObjectType并访问方法“.getFields()” . 最后我想要进行以下测试 .

expect(categoryType.getFields()).to.have.property('name');
expect(categoryType.getFields().name.type).to.deep.equals(graphql.GraphQLString);

1 回答

  • 0

    好的,解决方案比我预想的更容易 . 我们只需要将getType的返回值转换为TypeScript中的GraphQLObjectType .

    所以每当有人遇到同样的问题时 .

    describe('Category Schema', () => {
        it('Should have an Name field of type String', () => {
            let testType = schema.getType('Category') as GraphQLObjectType;
    
            expect(testType.getFields()).to.have.property("name");
            expect(testType.getFields().name.type).to.deep.equals(GraphQLString)
        })
    })
    

相关问题