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 });

Above example is working well, but I want to use a mutation name increase_length instead of increase_fish_length and increase_rope_length.

I tried to name mutations Fish/increase_length and Rope/increase_length using slash, but it didn't work. (only /[_A-Za-z][_0-9A-Za-z]*/ are available.)

Dose GraphQl support any solution for namespace?

left click
  • 894
  • 10
  • 21

2 Answers2

1

Graphql doesn't support something like namespace

yussenn
  • 577
  • 1
  • 7
  • 11
1

I've been toying around with some ideas around namespaces. What if your typeDefinitions looked something like this:

type Mutation {
    increase_length: IncreaseLengthMutation!
}

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

and your resolvers looked like this:

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

The biggest drawback is the wonky mutation resolver that returns an empty array. It has to exist so that it cascades down to the other mutations, though.

Dan Crews
  • 3,067
  • 17
  • 20
  • Yeah~! I used to apply this idea to Query in the past. I have to consider it. Thanks for sharing your idea! – left click Aug 24 '18 at 15:03