graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

[resolvers-composition] Use Namespace-types without depth limit

Open Drilmo opened this issue 3 years ago • 3 comments

Issue workflow progress

Progress of the issue based on the Contributor Workflow

  • [ ] 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox

    Make sure to fork this template and run yarn generate in the terminal.

    Please make sure the GraphQL Tools package versions under package.json matches yours.

  • [ ] 2. A failing test has been provided
  • [ ] 3. A local solution has been provided
  • [ ] 4. A pull request is pending review

Describe the bug

When I use namespace-types I can't use resolvers-composition, because the resolver-composition does not allow depth, but only : ${typeName}.${field}

Namespace-types : https://graphql-rules.com/rules/mutation-namespaces

Code line : https://github.com/ardatan/graphql-tools/blob/master/packages/resolvers-composition/src/resolvers-composition.ts#L69

To Reproduce Steps to reproduce the behavior:

Namespace-types : https://graphql-rules.com/rules/mutation-namespaces

and

https://www.graphql-tools.com/docs/resolvers-composition#supported-path-matcher-format

Expected behavior Based on the documentation: Capture d’écran 2022-08-01 à 16 11 05

If namespace-types are possible we should be able to do this kind of thing:

  • Query.User.*
  • Query.User.Group.*
  • Mutation.User.!{createUser, login}

Environment:

  • @graphql-tools/resolvers-composition:

Thank you in advance for your consideration and feedback

Drilmo avatar Aug 01 '22 14:08 Drilmo

Resolvers Composition follows the signature of resolvers which is always Type.field. So due to the resolution algorithm of GraphQL-js, I don't think it is possible to make this work in that way because each resolver is independent from the resolution of the parent.

I am not sure "namespace-types" is part of GraphQL Spec or GraphQL-js implementation. It seems too specific for a library. But I'm open for contributions if it is something we can support in GraphQL Tools without adding a dependency or breaking the other users.

ardatan avatar Aug 01 '22 14:08 ardatan

Hi @ardatan,

I managed to switch to Debug mode, and the namespace type is usable in this example:

const typeDefs = gql`
  type Mutation {
    article: ArticleMutations
  }

   type Query {
    article: ArticleQuery
  }

  type ArticleMutations {
    like: Boolean
    unlike: Boolean
  }

  type ArticleQuery {
    article(id: ID): Article
    articles: [Article!]
  }

`;

const resolvers = {
  Mutation: {
    article: () => ({}), // namespace-type
  },
  ArticleMutations: {
    like: () => { /* resolver code */ },
    unlike: () => { /* resolver code */ },
  },
  Query: {
    article: () => ({}), // namespace-type
  },
  ArticleQuery: {
    article: (id) => { /* resolver code */ },
    articles: () => { /* resolver code */ },
  },
};

it just needs to be written like this:

  • ArticleMutations.*
  • ArticleMutations.like
  • ArticleMutations.!like
  • ArticleQuery.*
  • ArticleQuery.article

I think an update of the doc would be good for users.

thank you in advance for your feedback

Drilmo avatar Aug 03 '22 08:08 Drilmo

Oh now I see what you mean. Would you create a PR for the docs? I think it would be nice to have it written by your perspective.

ardatan avatar Aug 03 '22 09:08 ardatan