data-client icon indicating copy to clipboard operation
data-client copied to clipboard

GraphQL ergonomics

Open ntucker opened this issue 2 years ago • 1 comments

This is explore possible ergonomics improvements to GraphQL support. For context, the initial library was built to support 100% of the powers of Rest Hooks high performance high data integrity with helpers built on the low level API. To expand upon GraphQL support more specializations can be created on top of. These of course will always be completely optional.

https://ntucker.notion.site/GQLResource-3879592f59a044f5a461f91ed0cbfb9e

ntucker avatar Apr 22 '22 22:04 ntucker

Before

export const userDetail = gql.query(
  (v: { name: string }) => `query UserDetail($name: String!) {
    user(name: $name) {
      id
      name
      email
    }
  }`,
  { user: User },
);

After

export const userDetail = gql.query(
  'user ',
  { name: 'String' },
  User,
);

Before

const createReview = gql.mutation(
  (v: {
    ep: string;
    review: { stars: number; commentary: string };
  }) => `mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
    createReview(episode: $ep, review: $review) {
      stars
      commentary
    }
  }`,
  { createReview: Review },
);

After

const createReview = gql.mutation(
  'createReview',
 { episode; 'Episode!', review: 'ReviewInput!' },
  Review,
);

Types can be registered in the construction of base endpoint

const gql = new GQLEndpoint(
  'https://swapi-graphql.netlify.app/.netlify/functions/index',
 { schema: { 'ReviewInput': { stars: 'Number', commentary: 'String' } }},
);

ntucker avatar Oct 10 '22 18:10 ntucker

Closing in favor of discussion: https://github.com/data-client/rest-hooks/discussions/2403

ntucker avatar Jan 31 '23 15:01 ntucker