Skip to content

Fix GraphQL String coercion #2990

Closed

Description

@andimarek

String parseValue should only accept Strings, nothing els.e

Activity

  1. bbakerman commented on Oct 19, 2022

    @bbakerman
    Member

    Citation needed

  2. bbakerman commented on Oct 19, 2022

    @bbakerman
    Member

    from graphql-js

    function coerceString(inputValue: mixed): string {
      if (typeof inputValue !== 'string') {
        throw new GraphQLError(
          `String cannot represent a non string value: ${inspect(inputValue)}`,
        );
      }
      return inputValue;
    }
    
    export const GraphQLString = new GraphQLScalarType({
      name: 'String',
      description:
        'The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.',
      serialize: serializeString,
      parseValue: coerceString,
      parseLiteral(valueNode) {
        if (valueNode.kind !== Kind.STRING) {
          throw new GraphQLError(
            `String cannot represent a non string value: ${print(valueNode)}`,
            valueNode,
          );
        }
        return valueNode.value;
      },
    });
    
    
    
  3. bbakerman commented on Oct 19, 2022

    @bbakerman
    Member

    This is a big behaviour change no?

  4. dondonz commented on Oct 19, 2022

    @dondonz
    Member

    To add more context, @andimarek noticed this while we had a chat about string-based custom scalars. This sub-category of custom scalars is relying on consistent String coercion across implementations. Hence, we should be in line with the JS implementation, although it is a behaviour change.

  5. self-assigned this
    on Nov 14, 2022
  6. added this to the 20.0 milestone on Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions