Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql-tag integration #8

Open
acro5piano opened this issue Dec 26, 2018 · 3 comments
Open

graphql-tag integration #8

acro5piano opened this issue Dec 26, 2018 · 3 comments
Labels
discussion Help your opinion

Comments

@acro5piano
Copy link
Owner

acro5piano commented Dec 26, 2018

I would like to keep this library thin, but every time writing

import gql from 'graphql-tag'

const getUser = graphqlify.query({ getUser: ... })

const Compoent = graphql(gql(getUser))({ data }: Props) => ...

is annoying, so it would be great to have gql compatible mode.

If we implement it, the code should be like this:

// in bootstrap
import { useGraphQLTag } from 'typed-graphqlify'
useGraphQLTag(true)

// in Component.ts
const getUser = graphqlify.query({ getUser: ... })

const Compoent = graphql(getUser)({ data }: Props) => ...
@acro5piano acro5piano added the discussion Help your opinion label Dec 26, 2018
@capaj
Copy link
Contributor

capaj commented Dec 26, 2018

agree. I'd say gql-tag usage is more probable than a user wanting a plain string output. So it should default to returning gql-tag.

I'd model it something like:

const getUserGqlTag = graphqlify.query({ getUser: ... })
const getUserString = graphqlify.query.asString({ getUser: ... })

because then user can choose string/gql-tag for each individual query.

@acro5piano
Copy link
Owner Author

Thank you for your response.

asString property looks flexible.
I will try to implement it after #10 merged!

@hcharley
Copy link

hcharley commented Sep 30, 2022

fwiw, I use this little helper:

/* ./lib/to-operation.ts */
import { gql } from '@apollo/client';
import { CompiledResult } from 'typed-graphqlify';

export const toOperation = <D, V>(value: CompiledResult<D, V>) =>
  gql(value.toString());
/* ./lib/gql-operation.ts */
import { CompiledResult } from 'typed-graphqlify';
import { toOperation } from './to-operation';

export class GqlOperation<D, V> {
  constructor(public tag: CompiledResult<D, V>) {}

  get type(): D {
    return this.tag.result.data;
  }

  get operation() {
    return toOperation(this.tag);
  }
}
/* ./domain/post.ts */
import { query, types } from 'typed-graphqlify';
import { GqlOperation } from '../lib/gql-operation';

export const getPostQuery = new GqlOperation(
  query('getPost', {
    post: {
      name: types.string,
    },
  })
);

export const getPostsQuery = new GqlOperation(
  query('getPosts', {
    post: {
      name: types.string,
    },
  })
);
export function PostList(props: PropsWithChildren<PostListProps>) {
  const { loading, error, data } = useQuery<typeof getPostsQuery.type>(
    getPostsQuery.operation
  );

  return (
    <StyledPostList>
      <h2>Post List</h2>
      {JSON.stringify({ loading, error, data })}
      {props.children}
    </StyledPostList>
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Help your opinion
Projects
None yet
Development

No branches or pull requests

3 participants