1
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_lengthincrease_rope_length

我尝试使用斜线命名突变Fish/increase_lengthRope/increase_length,但它不起作用。(仅 /[_A-Za-z][_0-9A-Za-z]*/ 可用。)

Dose GraphQl 支持命名空间的任何解决方案吗?

4

2 回答 2

1

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

于 2018-08-18T14:23:41.250 回答
1

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

type Mutation {
    increase_length: IncreaseLengthMutation!
}

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

你的解析器看起来像这样:

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

最大的缺点是返回空数组的不稳定的变异解析器。不过,它必须存在,才能级联到其他突变。

于 2018-08-21T01:08:59.460 回答