Comments (9)
@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.
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.
@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.
@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.
@AdamBrodzinski Note that PR #89 has now been created to address this more directly.
from absinthe.
@bruce wow you guys are the best! 👍 Keep up the great work!
from absinthe.
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.
- 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.
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)
- Handle Unknown Exceptions, Process Crashes and provide respective errors with GraphQL HOT 2
- mix absinthe.schema.sdl does not include `default_values`
- extend schema cannot parse maps in arguments HOT 1
- How to use Decimal type to display normal format of serialize HOT 1
- Create release tags for 1.7.* releases HOT 1
- Schema compilation with absinthe 1.7.1 generates warnings with elixir version < 1.14 HOT 11
- Dialyzer error when expecting "subscribed" key returned from Absinthe.run/3
- Subscriptions don't trigger with namespaced mutations
- Support deprecated input object fields in introspection query
- How do create multiple inputs ? In Mutations ? HOT 1
- Huge max_heap_size compared to the actual data size HOT 2
- "Unknown Registry" when publishing during node startup or shutdown HOT 6
- Subscription fails to resolve using default resolver if it there is a middleware specified HOT 2
- Allow subscriptions to listen to "regular" PubSub events HOT 1
- False positive error for non_null validation HOT 2
- Using GraphQL (and perhaps Absinthe) in messages? HOT 3
- small CSS issue on the Absinthe landing page HOT 3
- add support for `config` functions (on subscriptions) in hydrate/2
- Resolving field of type `[[Value]!]!]` seems to fail with valid values HOT 2
- Compilation infinite loop leaking memory HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from absinthe.