0

Right now I am doing this:

import { comment } from "forms";

const QUERY_COMMENTS = gql`
  query QUERY_COMMENTS($projectId:Int! $orderBy:CommentOrderByInput) {
    comments(projectId:$projectId orderBy:$orderBy) {
      ${comment}
    }
  }
`;

export const comment = `
    id
    text
    date
    postedBy {
        id
        name
    }
`;

interface comment {
     id:number,
     text:string,
     date:string,
     postedBy: {
       id:number,
       name:string,
     },
   }

I basically repeat myself.
IMO it should be possible and be very useful to use typescript definitions in graphql-queries.
Yet I did not find a way to do it.

2
  • 1
    Typescript definitions get erased when you transpile your code into JS, so no, you can't use them in any way at runtime Commented Oct 24, 2020 at 15:33
  • 1
    Maybe type-graphql is worth a look. Commented Oct 24, 2020 at 16:59

2 Answers 2

1

you can use graphql-zeus.

this library generates typescript definitions from api and gives you some method to use for queries and mutations. methods are using definitions that i mentioned, so you are typesafe.

Sign up to request clarification or add additional context in comments.

Comments

1

From the tags on your question, it looks like you're using the Apollo ecosystem. So i'd looking into their codegen support: https://github.com/apollographql/apollo-tooling#code-generation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.