1

is it possible to build graphql queries using existing interfaces? or we should write same code twice)

for example in the doc we have

interface Post {
  id: string;
  title: string;
  ...

and then writing same fields in query

query allPosts {
      posts {
        id
        title
        ...

is there any way to write something like

query allPosts {
      posts: Post[]
Pasha K
  • 357
  • 1
  • 7

2 Answers2

2

It's not possible to do something like that because TypeScript types don't exist at runtime. Interfaces are also insufficient to represent GraphQL fields because they can't capture information like argument values or directive usage.

You can, however, auto-generate TypeScript types from existing queries, which is what is normally done. See GraphQL Code Generator.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
1

NO ... types!=queries ... posts: Post[] already used in response definition

Query defines what subset (fields) you need from this (and other) type(-s), f.e. only title

You're probably looking for Fragments

xadm
  • 8,219
  • 3
  • 14
  • 25