-
Notifications
You must be signed in to change notification settings - Fork 5
Directives
Ahrim Yang edited this page Nov 30, 2020
·
6 revisions
์์ฑ์: J113 ์์๋ฆผ
- ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฟผ๋ฆฌ์ ๊ตฌ์กฐ์ ํํ๋ฅผ ๋์ ์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ ์ํ GraphQL ์ ๊ธฐ๋ฅ
- version ๊ด๋ฆฌ๋ฅผ
@deprecated
๋ฑ์ ํตํด์ ์ฝ๊ฒ ํ ์ ์๋ค. - field ๋ fragment๋ฑ์ ์ฝ์ ๋ ์ ์๋ค.
- Directives๊ฐ ์ฝ์ ๋๋ ์์์ ๋ฐ๋ผ์ ๋์์ด ๋ฌ๋ผ์ง๋ค.
- Directive Definition: Directives ์ ์ ๋ฐฉ๋ฒ
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
# FIELD
query skipField($someTest: Boolean!) {
users{
name
age
}
books{
title
author @skip(if: $someTest)
}
}
# INLINE_FRAGMENT
query skipFragmentInline($someTest: Boolean!) {
users{
name
age
}
books @skip(if: $someTest){
title
author
}
}
# FRAGMENT_SPREAD
query skipFragment($someTest: Boolean!) {
users{
...userField
}
books @skip(if: $someTest){
...bookField
}
}
fragment userField on User{
name
age
}
fragment bookField on Book{
title
author
}
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
# FIELD
query skipField($someTest: Boolean!) {
users{
name
age
}
books{
title
author @include(if: $someTest)
}
}
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE
#FIELD_DEFINITION
type ExampleType {
newField: String
oldField: String @deprecated(reason: "Use 'newField'.")
}
#ENUM_VALUE
enum AllowedColor {
RED @deprecated
GREEN
BLUE
}
- Apollo server๊ฐ ์ง์ํ๋
schemaDirectiveVisitor
๋ฅผ ์ฌ์ฉํ์ฌ custom directive๋ฅผ ์ ์ํ ์ ์๋ค. - Implementing directives Examples: Custom Directives ์์
// Directive ์ ์
class UpperCaseDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
//FiedlDefintion์ ๋ํ ์ ์
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
const result = await resolve.apply(this, args);
if (typeof result === 'string') {
return result.toUpperCase();
}
return result;
};
}
}
// Directive ์์ฑ ๋ฐ ์ฌ์ฉ
const typeDefs = gql`
directive @upper on FIELD_DEFINITION
type Query {
hello: String @upper
}
`;
//resolver
const resolvers = {
Query: {
hello: (parent, args, context) => {
return 'Hello world!';
},
},
};
//ApolloServer์ custom directive ์ถ๊ฐ
const server = new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {
upper: UpperCaseDirective,
upperCase: UpperCaseDirective
//ํ๋์ custom Directive class๋ฅผ ์ฌ๋ฌ๊ฐ์ directives๋ก ์ถ๊ฐ ํ ์ ์์
}
});
-
Schema directives:
graphql-tools
๋ฅผ ์ด์ฉํ custom directives
๐กHome
- Apollo References
- Schema Directives
- Apollo Client - Local State
- GraphQL Execution
- Apollo Server Execution
- Apollo Client Cache
- Apollo Client Execution
- Mongoose-Populate