Skip to content

Commit

Permalink
add comments, snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Oct 28, 2024
1 parent ab78a30 commit e20832b
Show file tree
Hide file tree
Showing 7 changed files with 1,083 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@
"scopeName": "inline.connectors.graphql",
"path": "./syntaxes/graphql.connectors.json",
"embeddedLanguages": {
"meta.embedded.block.jsonselection.graphql": "apollo.jsonselection"
"meta.embedded.block.jsonselection.graphql": "apollo.jsonselection",
"meta.embedded.block.string.jsonselection.graphql": "apollo.jsonselection"
}
},
{
Expand Down
10 changes: 0 additions & 10 deletions sampleWorkspace/rover/src/test.apollo.jsonselection

This file was deleted.

3 changes: 3 additions & 0 deletions sampleWorkspace/sampleWorkspace.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
},
{
"path": "../src/language-server/__tests__/fixtures/documents"
},
{
"path": "../src/__tests__/fixtures"
}
],
"settings": {}
Expand Down
137 changes: 137 additions & 0 deletions src/__tests__/fixtures/test.apollo.jsonselection
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Examples from https://github.com/apollographql/router/blob/next/apollo-federation/src/sources/connect/json_selection/README.md

id
name
abc: some.nested.path { a b c }
id
name
some.nested.path { a b c }
names: {
first: firstName
last: lastName
}
# Also allowed:
firstName
lastName
postID
title
author: {
id: authorID
name: authorName
}
title
year: publication.year
authorName: author.name
id
created
model

# The { role content } SubSelection is mandatory so the output keys
# can be statically determined:
choices->first.message { role content }

# Multiple PathWithSubSelections are allowed in the same SubSelection:
choices->last.message { lastContent: content }
# For some reason /users/{$args.id} returns an object with name
# and email but no id, so we inject the id manually:
id: $args.id
name
email
id name friends: friend_ids { id: $ }
items: data.nested.items { id name }
firstItem: data.nested.items->first { id name }
firstItemName: data.nested.items->first.name
$.data { id name }
id: data.id
name: data.name
all: $.first->and($.second)->and($.third)
wrapped: field->echo({ fieldValue: @ })
children: parent->echo([@.child1, @.child2, @.child3])
doubled: numbers->map({ value: @->mul(2) })
types: values->map(@->typeof)
__typename: $("Product")
condition: $(true)
alphabetSlice: $("abcdefghijklmnopqrstuvwxyz")->slice($args.start, $args.end)
suffix: results.slice($(-1)->mul($args.suffixLength))
# The ->echo method returns its first input argument as-is, ignoring
# the input data. Useful for embedding literal values, as in
# $->echo("give me this string"), or wrapping the input value.
__typename: $->echo("Book")
wrapped: field->echo({ fieldValue: @ })

# Returns the type of the data as a string, e.g. "object", "array",
# "string", "number", "boolean", or "null". Note that `typeof null` is
# "object" in JavaScript but "null" for our purposes.
typeOfValue: value->typeof

# When invoked against an array, ->map evaluates its first argument
# against each element of the array, binding the element values to `@`,
# and returns an array of the results. When invoked against a non-array,
# ->map evaluates its first argument against that value and returns the
# result without wrapping it in an array.
doubled: numbers->map(@->mul(2))
types: values->map(@->typeof)

# Returns true if the data is deeply equal to the first argument, false
# otherwise. Equality is solely value-based (all JSON), no references.
isObject: value->typeof->eq("object")

# Takes any number of pairs [candidate, value], and returns value for
# the first candidate that equals the input data. If none of the
# pairs match, a runtime error is reported, but a single-element
# [<default>] array as the final argument guarantees a default value.
__typename: kind->match(
["dog", "Canine"],
["cat", "Feline"],
["Exotic"]
)

# Like ->match, but expects the first element of each pair to evaluate
# to a boolean, returning the second element of the first pair whose
# first element is true. This makes providing a final catch-all case
# easy, since the last pair can be [true, <default>].
__typename: kind->matchIf(
[@->eq("dog"), "Canine"],
[@->eq("cat"), "Feline"],
[true, "Exotic"]
)

# Arithmetic methods, supporting both integers and floating point values,
# similar to JavaScript.
sum: $.a->add($.b)->add($.c)
difference: $.a->sub($.b)->sub($.c)
product: $.a->mul($.b, $.c)
quotient: $.a->div($.b)
remainder: $.a->mod($.b)

# Array/string methods
first: list->first
last: list->last
index3: list->get(3)
secondToLast: list->get(-2)
slice: list->slice(0, 5)
substring: string->slice(2, 5)
arraySize: array->size
stringLength: string->size

# Object methods
aValue: $->echo({ a: 123 })->get("a")
hasKey: object->has("key")
hasAB: object->has("a")->and(object->has("b"))
numberOfProperties: object->size
keys: object->keys
values: object->values
entries: object->entries
keysFromEntries: object->entries.key
valuesFromEntries: object->entries.value

# Logical methods
negation: $.condition->not
bangBang: $.condition->not->not
disjunction: $.a->or($.b)->or($.c)
conjunction: $.a->and($.b, $.c)
aImpliesB: $.a->not->or($.b)
excludedMiddle: $.toBe->or($.toBe->not)->eq(true)

# Tests from https://github.com/apollographql/router/blob/next/apollo-federation/src/sources/connect/url_template.rs

Loading

0 comments on commit e20832b

Please sign in to comment.