Skip to content

Commit

Permalink
Return an interface with multiple weak concrete types from a resolver…
Browse files Browse the repository at this point in the history
… field

Reviewed By: captbaritone

Differential Revision: D56438692

fbshipit-source-id: 12a4a029004f0b17cc71f6919f19597a63824afe
  • Loading branch information
monicatang authored and facebook-github-bot committed Apr 24, 2024
1 parent 46879b4 commit 55b8716
Show file tree
Hide file tree
Showing 6 changed files with 476 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/react-relay/__tests__/RelayResolverInterface-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,45 @@ describe.each([
},
);

describe.each([
{
animalType: 'Octopus',
numLegs: '8',
},
{
animalType: 'PurpleOctopus',
numLegs: '8',
},
])(
'resolvers can return an interface where all implementors are weak model types: %s',
({animalType, numLegs}) => {
function WeakAnimalLegsQueryComponent(props: {request: {ofType: string}}) {
const data = useClientQuery(
graphql`
query RelayResolverInterfaceTestWeakAnimalLegsQuery(
$request: WeakAnimalRequest!
) {
weak_animal(request: $request) {
...RelayResolverInterfaceTestWeakAnimalLegsFragment
}
}
`,
{request: props.request},
);
return <WeakAnimalLegsFragmentComponent animal={data.weak_animal} />;
}

test(`should read the legs of a ${animalType}`, () => {
const animalRenderer = TestRenderer.create(
<EnvironmentWrapper environment={environment}>
<WeakAnimalLegsQueryComponent request={{ofType: animalType}} />
</EnvironmentWrapper>,
);
expect(animalRenderer.toJSON()).toEqual(numLegs);
});
},
);

test('resolvers can return a list of interfaces where all implementors are strong model types', () => {
function AnimalsLegsQueryComponent(props: {
requests: Array<{ofType: string, returnValidID: boolean}>,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
* @oncall relay
*/

import type {Query__weak_animal$normalization} from 'relay-runtime/store/__tests__/resolvers/__generated__/Query__weak_animal$normalization.graphql';

/**
* Returns a single `IWeakAnimal` of a given type.
*
* @RelayResolver Query.weak_animal(request: WeakAnimalRequest!): IWeakAnimal
*/
function weak_animal(args: {
request: {ofType: string},
}): Query__weak_animal$normalization {
switch (args.request.ofType) {
case 'Octopus':
return {
__relay_model_instance: {},
__typename: 'Octopus',
};
case 'PurpleOctopus':
return {
__relay_model_instance: {},
__typename: 'PurpleOctopus',
};
default:
throw new Error('Invalid type');
}
}

/**
* Returns a list of `IWeakAnimal` of a given type.
*
* @RelayResolver Query.weak_animals(requests: [WeakAnimalRequest!]!): [IWeakAnimal]
*/
function weak_animals(args: {
requests: $ReadOnlyArray<{ofType: string}>,
}): Array<Query__weak_animal$normalization> {
return args.requests.map(request => {
return weak_animal({request});
});
}

module.exports = {
weak_animal,
weak_animals,
};
Loading

0 comments on commit 55b8716

Please sign in to comment.