Jeff Lowery
Jeff Lowery

Reputation: 2597

Is there a way to pass a fragment to graphiql?

It's possible to pass a query, but apparently not a fragment:

server.use('/graphiql', graphiqlExpress({
    endpointURL: '/graphql',
    query: `# Welcome to GraphiQL

query PostsForAuthor {
  author(id: 1) {
    firstName
    posts {
      title
      votes
    }
  }
}`}));


Update 10/12/2017 It is possible to send fragments along with a query using Apollo's client:

http://dev.apollodata.com/core/fragments.html

This is not a solution to the original question, however; I would like to pass fragments to a graphiql server instance at startup.

Upvotes: 5

Views: 3548

Answers (2)

galki
galki

Reputation: 8715

by startup do you mean from the server? if so I don't believe that's how fragments are used. my understanding is as follows:

  1. on the server you provide Types (like User)
  2. on the client you query those Types using queries and fragments

for instance, if you provide type User on the server, on the client graphQL you can use fragments to query that type:

graphQL (client)

fragment authorData on AuthorType{
  firstName
  posts {
    title
    votes
  }
}

query PostsForAuthor {
  author(id: 1) {
    ...authorData
  }
}

Upvotes: 1

machineghost
machineghost

Reputation: 35770

As you noticed (and as detailed here) GraphiQL takes a query argument:

query: an optional GraphQL string to use as the initial displayed query, if undefined is provided, the stored query or defaultQuery will be used.

If putting a fragment in as the value for that argument doesn't work, then I don't believe there is any way to start with a fragment ... but really why would you even want to? A fragment by itself isn't executable, and the whole idea is to start GraphiQL with a (executable) query.

If all you want is to be able to copy/paste in some text that you use frequently in your queries, a bookmarklet might be a better idea.

Upvotes: 0

Related Questions