graphqlapollographql-tag

Defining JSON/Object type in graphql-tag


I'm kinda new to Apollo gql, just wondering if anyone know if its possible to define Object class in graphql-tag?

export const CREATE_STYLE = gql`
  mutation styleCreate(
    $formID: String!
    $fontFamily: Object //how do you define object/JSON object?
  ) {
    styleCreate(
      formID: $formID
      fontFamily: $fontFamily

    ) {
      styleID

    }
  }
`;


Solution

  • First, if the input type is an object I would recommend to define that on the server as a input type.

    In my setup I'm using:

    export const createUser = gql`
        mutation createUser($user: UserCreate) {
            create(input: $user) {
                name
                email
            }
        }
    

    where "UserCreate" is an interface that looks like:

    export interface UserCreate {
      // The user name.
      name: string,
      // The user email address.
      email: string,
    };
    

    You can manually create the interface, but I would suggest to use apollo codegen that gives you all the input types you need.