首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphQL拼接-为什么子模式中的字段返回null?

GraphQL拼接-为什么子模式中的字段返回null?
EN

Stack Overflow用户
提问于 2021-01-04 13:57:19
回答 1查看 310关注 0票数 0

我试图将两个GraphQL模式缝合在一起,一个来自内容模式,另一个来自neo4j模式。

在跨组合模式的查询过程中,每个子模式似乎都会被查询,但是“外域”字段总是以null的形式返回。

我就是搞不懂这件事。

示例查询:

代码语言:javascript
复制
query {
  //Request data exclusively from the neo4j schema
  Product(id:"475e006f-b9cf-4f40-8712-271ceb46d14b"){
    id,
    name,
    weight
  },
  //This is a contentful schema query which should return weight from neo4j
  product(id:"[contentful-native-id]"){
      id,
      weight, 
  }
}

结果:

代码语言:javascript
复制
  "data": {
    "Product": [
      {
        "id": "475e006f-b9cf-4f40-8712-271ceb46d14b",
        "name": "Test product name",
        "weight": 14.9
      }
    ],
    "product": {
      "id": "475e006f-b9cf-4f40-8712-271ceb46d14b",
      "weight": null //This shouldn't be null! :(
    }
  }

日志记录:

代码语言:javascript
复制
//First query being executed against neo4j database
neo4j-graphql-js MATCH (`product`:`Product` {id:$id}) RETURN `product` { .id , .name , .weight } AS `product`
neo4j-graphql-js {
   "offset": 0,
   "first": -1,
   "id": "475e006f-b9cf-4f40-8712-271ceb46d14b"
 }

//Triggered by the second query correctly trying to resolve weight from neo4j
neo4j-graphql-js MATCH (`product`:`Product` {id:$id}) RETURN `product` { .weight , .id } AS `product`
neo4j-graphql-js {
   "offset": 0,
   "first": -1,
   "id": "475e006f-b9cf-4f40-8712-271ceb46d14b"
}

这似乎表明有什么东西是有效的,但重量的结果永远无法达到最终的输出。ApolloServer不通过didEncounterErrors()报告任何错误

拼接:

代码语言:javascript
复制
const gatewaySchema = stitchSchemas({
    subschemas: [{
            schema: neoSchema,
            merge: {
                Product: {
                    selectionSet: '{id}',
                    fieldName: 'Product',
                    args: ({
                        id
                    }) => ({
                        id
                    }),
                }
            }
        },
        {
            schema: contentfulSchema,
            merge: {

            }
        }
    ],
})

模式:

代码语言:javascript
复制
const executor = async ({
    document,
    variables,
    context
}) => {
    const query = print(document);
    //console.log(query);
    const fetchResult = await fetch('https://graphql.contentful.com/content/v1/spaces/[SPACE]', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer [AUTHTOKEN]`,
        },
        body: JSON.stringify({
            query,
            variables
        })
    });
    return fetchResult.json();
};

const contentfulSchema = wrapSchema({
    schema: await introspectSchema(executor),
    executor: executor
});

const driver = neo4j.driver(
    process.env.NEO4J_URI || 'bolt://localhost:7687',
    neo4j.auth.basic(
        process.env.NEO4J_USER,
        process.env.NEO4J_PASS
    ), {
        encrypted: process.env.NEO4J_ENCRYPTED ? 'ENCRYPTION_ON' : 'ENCRYPTION_OFF',
    }
)

const neoSchema = makeAugmentedSchema({
    typeDefs: typeDefs,

});

服务器:

代码语言:javascript
复制
 const server = new ApolloServer({
      schema: gatewaySchema,
      context: ({ req }) => {
        return {
          driver,
          req
        };
      },
      plugins:[
        myPlugin
      ]
    });

任何见解或想法都非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2021-01-04 16:02:26

这似乎是因为stitchSchemas在ApolloServer中不受支持.

Does Apollo Server work with GraphQL Tools stitchSchemas?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65563891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档