josemarluedke / glimmer-apollo

Ember and Glimmer integration for Apollo Client.
https://glimmer-apollo.com
MIT License
38 stars 14 forks source link

Fragments support #59

Closed billdami closed 2 years ago

billdami commented 2 years ago

Are graphql fragments supported out-of-the-box in glimmer-apollo? I tried creating one with an inline gql tag and using it in queries, however when the graphql request is called, the fragment is not expanded, so the query sent to the server contains the ...MyFragment line, instead of the fields it contains, and I get an Unknown fragment error back from the server.

For example:

export const MY_FRAGMENT = gql`
    fragment MyFragment on User {
        id
        dateCreated
        name
    }
`;

export const GET_USERS = gql`
    query GetUsers($filters: UserFilters!) {
        users(filters: $filters) {
            ...MyFragment
        }
    }
`;

export const GET_USER = gql`
    query GetUser($id: Int!) {
        user(id: $id) {
            ...MyFragment
            someOtherField
        }
    }
`;

^ Should that work?

josemarluedke commented 2 years ago

Hi @billdami. Yes, fragments are supported. However, you need to inline them with the query. Here is how that would look like:

export const MY_FRAGMENT = gql`
    fragment MyFragment on User {
        id
        dateCreated
        name
    }
`;

export const GET_USERS = gql`
    query GetUsers($filters: UserFilters!) {
        users(filters: $filters) {
            ...MyFragment
        }
    }
  ${MY_FRAGMENT}
`;

export const GET_USER = gql`
    query GetUser($id: Int!) {
        user(id: $id) {
            ...MyFragment
            someOtherField
        }
    }
  ${MY_FRAGMENT}
`;

I hope this solves your issue.

billdami commented 2 years ago

makes sense, thank you!