Is it possible to write additional GraphQL query fields after importing a schema in Absinthe?

I have a schema setup like so:

defmodule DxAppRcWeb.Schema do
  use Absinthe.Schema

  import_sdl(path: "assets/gql/protected-schema.graphql")

def hydrate(%{identifier: :upsert_organization}, [%{identifier: :mutation} | _]),
    do: {:resolve, &DxAppRc.Resolvers.upsert_organization/3}

# Etc...

I want to add a field to the schema, and I want to start using Absinthe’s native schema design tools (and incrementally transition away from my SDL being the Schema’s source of truth)

I tried adding this to the file above:


  query do
    @desc "Change Staff Case Permissions"
    field :change_staff_permission, :clinical_mutation_response do
      arg(:input, non_null(:change_staff_permission))
      resolve(DxAppRc.Resolvers.change_staff_permission() / 3)

I received an error that query is not a unique object. Is there a way to append to the query definition in the SDL file?

In the latest release of Absinthe support for extending objects was added. You can use the extend/2 macro to do this.

You can look into the macro_extensions_test.exs for examples on how it works and what you can do with it. It should follow the extend syntax of the graphql spec.

This addition to Absinthe is new so if you run into any issues let me know so I can look into it.


Thanks! I’ll take a look!

1 Like

Oh good to know thanks, been wanting this for ages :slight_smile:

Could you explain what is extend? How it differs from unions and interfaces (I guess extend has a completely different purpose)?

Unions and interfaces are tools within the graphql schema definition language to define complex types.

Absinthe extend allows you to define part of the schema in an sdl file, and another part using schema notation in the schema.ex file. Without the macro, there is a conflict that you cannot define an object (like query) in two different locations.

1 Like