Git Product home page Git Product logo

Comments (9)

bruce avatar bruce commented on May 17, 2024

@AdamBrodzinski Thanks for trying out Absinthe. I'll try to help.

At the moment, I can see two paths to make this work.

Option 1: Use the Passthrough adapter

One option is to use the passthrough adapter and define your schema with camelized field names (it sounds like this is the path you went down, except you didn't know about the adapter setting.

We have a guides on adapters at http://absinthe-graphql.org/guides/adapters/; the adapter you're looking for is Absinthe.Adapter.Passthrough:

config :absinthe,
  adapter: Absinthe.Adapter.Passthrough

Option 2: Define resolvers that look-up the values by the correct name

The default resolver for a field is a Map.get on the current object (source) using the field name. While we don't have a facility yet to override the default field resolver, you could resolve this behavior on a case-by-case basis, eg, given this schema:

  defmodule CamelizedFieldSchema do
    use Absinthe.Schema

    @user %{
      "verificationCode" => "abcdef",
      "anotherField" => "ghijk"
    }

    query do
      field :current_user, :user do
        resolve fn
          _, _ ->
            {:ok, @user}
        end
      end
    end

    @desc "A registered user"
    object :user do
      field :verification_code, :string, resolve: &camelized_resolve/2
      field :another_field, :string, resolve: &camelized_resolve/2
    end

    def camelized_resolve(_args, %{definition: %{name: field_name}, source: source}) do
      camelized_name = Absinthe.Utils.camelize(field_name, lower: true)
      {:ok, Map.get(source, camelized_name)}
    end
  end

This assertion passes:

    assert {:ok, %{data: %{"currentUser" => %{"verificationCode" => "abcdef", "anotherField" => "ghijk"}}}} = Absinthe.run("{ currentUser { verificationCode anotherField }}", CamelizedFieldSchema)

Conclusion

I'd go with Option 2 (I like Elixir code to look like Elixir code); we can investigate the best way for us to make the default resolver configurable.

from absinthe.

bruce avatar bruce commented on May 17, 2024

Notably, btw, Introspection currently doesn't work when you're using the Passthrough adapter (since its schema components are defined in snake_case) -- another point in Option 2's favor.

from absinthe.

bruce avatar bruce commented on May 17, 2024

@AdamBrodzinski I'm going to close this now; feel free to reopen or create more issues as you run into problems. Thanks for bringing this up, it's an enhancement worth addressing.

Note also that we're available on IRC and Slack (where we have our own channel); details here: http://absinthe-graphql.org/community/ if that format would better help you work through any problems.

from absinthe.

AdamBrodzinski avatar AdamBrodzinski commented on May 17, 2024

@bruce thanks for the help! I'll go with option two, seems like the best tradeoff. I'll checkout the Slack channel for the future.
🍻

from absinthe.

bruce avatar bruce commented on May 17, 2024

@AdamBrodzinski Note that PR #89 has now been created to address this more directly.

from absinthe.

AdamBrodzinski avatar AdamBrodzinski commented on May 17, 2024

@bruce wow you guys are the best! 👍 Keep up the great work!

from absinthe.

conradwt avatar conradwt commented on May 17, 2024

I'm having the same issue when using option 2 above:

query:

{
  person(id: "1") {
    firstName
    lastName
  }
}

response:

{
  "errors": [
    {
      "message": "Field `lastName': Not present in schema",
      "locations": [
        {
          "line": 4,
          "column": 0
        }
      ]
    },
    {
      "message": "Field `firstName': Not present in schema",
      "locations": [
        {
          "line": 3,
          "column": 0
        }
      ]
    }
  ],
  "data": {
    "person": {}
  }
}

from absinthe.

bruce avatar bruce commented on May 17, 2024

@conradwt:

  • If you're using the default (LanguageConventions) adapter, you still need to define your schema fields using snake_case, eg, field :first_name, :string. It is highly recommended you use the LanguageConventions adapter, as Passthrough breaks introspection.
  • You may also want to look at the default_resolve function, added since this issue was reported, explained under "Default Resolver" in the Absinthe.Schema documentation.
  • If you are still encountering problems please post a new issue with your schema and/or join us in our Slack room for assistance (details here).

from absinthe.

mmendez512 avatar mmendez512 commented on May 17, 2024

OPTION 1 - The Passthrough is a great option; however, the adapter is no long configurable in config.exs in 1.4; it's configurable via the Absinthe Plug when you set your route.

Here's the sample code:

forward "/graphiql", Absinthe.Plug.GraphiQL,
schema: MyAppWeb.Schema,
socket: MyAppWeb.Socket,
#prevents snake_case conversion of fields
adapter: Absinthe.Adapter.Passthrough,
interface: :simple

from absinthe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.