Server Side Fragments

Referencing:

Is there any update on being able to store Fragments on the Server Side specifically in the Slash? We are building out multiple UI to access the same DB. It would be nice if we didn’t have to redefine the fragments in each UI but rather could just expand fragments that are stored on the server side.

Proposed Schema

type Person {
  id: ID
  name: String
  hasFriends: [Person]
  hasAddresses: [Address]
}

type Address {
  id: ID
  line1: String
  city: String
  state: String
  zip: String
  country: String
}

fragment FullAddress on Address {
  id
  line1
  city
  state
  zip
  country
}

fragment AddressAndFriendWithAddress on Person {
  hasAddresses {
    ...FullAddress 
  }
  hasFriends {
    id
    name
    hasAddresses {
      ...FullAddress 
    }
  }
}

Then I could run a query like:

query {
  getPerson(id: ["0x2"]) {
    id
    name
    ...AddressAndFriendWithAddress 
  }
}

Without this, I have to send the fragments on the client side every time which looks like:

query {
  getPerson(id: ["0x2"]) {
    id
    name
    ...AddressAndFriendWithAddress 
  }
}
fragment FullAddress on Address {
  id
  line1
  city
  state
  zip
  country
}
fragment AddressAndFriendWithAddress on Person {
  hasAddresses {
    ...FullAddress 
  }
  hasFriends {
    id
    name
    hasAddresses {
      ...FullAddress 
    }
  }
}

If fragments doesn’t get added, to Slash GraphQL, is it possible to store them in npm package that can be shared across projects?

After all, fragments are client side constructs. (I think :sweat_smile:)

That would be great if everything we are making was javascript, lol. We are making a Swift app, a React app, and a Java/Kotlin app.

For our React app, we are talking right now about either finding a pre-made package to dynamically include fragments or writing our own hook to do so as a useFragment hook to apply to the query before passing it to the useQuery/useMutation hooks.

I thought about the possibility that you might be working on several platforms after giving that reply! :sweat_smile:

How about:

  1. Bootstrapping each app with fragments at build time? (Will save unnecessary call at runtime, & avoid overheads!)
  2. Or Using a service like Firebase Configuration? (Which will allow changing of application variables at runtime!)

P.S. I love the idea of having inline Fragments in Schema file as well.

1 Like

Hi,

I does look like a cool idea, but fragments aren’t allowed in the schema itself in GraphQL, just in the operations (the query/mutation requests).

I also wonder how it would work in your clients. For example, I think Apollo Client will need the fragment text on the client side in order to construct the query/mutation in GraphQL anyway. You could try this by removing the fragment defn and seeing what happens in useQuery. I expect it’ll fail. Given that, you still need the text on the client anyway.

I wonder if the simplest answer is a repo of fragments that’s shared across your projects?

2 Likes