首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何解决GraphQL中的命名冲突?

如何解决GraphQL中的命名冲突?
EN

Stack Overflow用户
提问于 2018-08-18 21:40:53
回答 2查看 431关注 0票数 1
代码语言:javascript
复制
import { makeExecutableSchema } from 'graphql-tools';

const fish = { length:50 },
      rope = { length:100 };

const typeDefs = `
    type Query {
        rope: Rope!
        fish: Fish!
    }

    type Mutation {
        increase_fish_length: Fish!
        increase_rope_length: Rope!
    }

    type Rope {
        length: Int!
    }

    type Fish {
        length: Int!
    }
`;

const resolvers = {
    Mutation: {
        increase_fish_length: (root, args, context) => {
            fish.length++;
            return fish;
        },
        increase_rope_length: (root, args, context) => {
            rope.length++;
            return rope;
        }
    }
};

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

上面的例子运行良好,但我想使用一个变异名称increase_length,而不是increase_fish_length和increase_rope_length。

我尝试使用斜杠将突变命名为Fish/increase_length和Rope/increase_length,但不起作用。(仅/_A-Za-z*/可用。)

GraphQl是否支持命名空间的解决方案?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-18 22:23:41

Graphql不支持名称空间之类的东西

票数 1
EN

Stack Overflow用户

发布于 2018-08-21 09:08:59

我一直在玩弄一些关于名称空间的想法。如果你的typeDefinitions看起来像这样会怎么样:

代码语言:javascript
复制
type Mutation {
    increase_length: IncreaseLengthMutation!
}

type IncreaseLengthMutation {
    fish: Fish!
    rope: Rope!
}

你的解算器看起来是这样的:

代码语言:javascript
复制
const resolvers = {
    Mutation: {
        increase_Length: () => {
            return {}
        }
    },
    IncreaseLengthMutation {
        fish: (root, args, context) => {
            fish.length++;
            return fish;
        },
        rope: (root, args, context) => {
            rope.length++;
            return rope;
        }
    }
};

最大的缺点是不可靠的突变解析器,它返回一个空数组。然而,它必须存在,这样它才能级联到其他突变。

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

https://stackoverflow.com/questions/51909282

复制
相关文章

相似问题

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