Open acro5piano opened 5 years ago
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.
Thank you for your response.
asString
property looks flexible.
I will try to implement it after #10 merged!
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>
);
}
I would like to keep this library thin, but every time writing
is annoying, so it would be great to have
gql
compatible mode.If we implement it, the code should be like this: