zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

strict_language_conventions.ex (1457B)


      1 defmodule Absinthe.Adapter.StrictLanguageConventions do
      2   @moduledoc """
      3   Strict version of `Absinthe.Adapter.LanguageConventions` that will reject
      4   improperly formatted external names.
      5 
      6   For example, this document:
      7 
      8   ```graphql
      9   {
     10     create_user(user_id: 2) {
     11       first_name
     12       last_name
     13     }
     14   }
     15   ```
     16 
     17   Would result in name-mismatch errors returned to the client.
     18 
     19   The client should instead send the camelcase variant of the names:
     20 
     21   ```graphql
     22   {
     23     createUser(userId: 2) {
     24       firstName
     25       lastName
     26     }
     27   }
     28   ```
     29 
     30   See `Absinthe.Adapter.LanguageConventions` for more information.
     31   """
     32 
     33   use Absinthe.Adapter
     34 
     35   @doc """
     36   Converts a camelCase to snake_case
     37 
     38   Returns `nil` if the converted internal name does not match the converted external name.
     39 
     40   See `Absinthe.Adapter.LanguageConventions.to_internal_name/2`
     41   """
     42   @impl Absinthe.Adapter
     43   def to_internal_name(external_name, role) do
     44     internal_name = Absinthe.Adapter.LanguageConventions.to_internal_name(external_name, role)
     45 
     46     if external_name == Absinthe.Adapter.LanguageConventions.to_external_name(internal_name, role) do
     47       internal_name
     48     else
     49       nil
     50     end
     51   end
     52 
     53   @doc """
     54   Converts a snake_case to camelCase
     55 
     56   See `Absinthe.Adapter.LanguageConventions.to_external_name/2`
     57   """
     58   @impl Absinthe.Adapter
     59   def to_external_name(internal_name, role) do
     60     Absinthe.Adapter.LanguageConventions.to_external_name(internal_name, role)
     61   end
     62 end