Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a custom Date scalar #1435

Closed
Renish-Development opened this issue Oct 5, 2020 · 12 comments · Fixed by #1474
Closed

Using a custom Date scalar #1435

Renish-Development opened this issue Oct 5, 2020 · 12 comments · Fixed by #1474
Labels
codegen Issues related to or arising from code generation

Comments

@Renish-Development
Copy link

@cerupcat @jzhw0130: You should be able to pass --passthrough-custom-scalars to apollo-codegen to avoid generating a typealias to String for custom scalars. You can then add your own alias and conversion code, see here for an example.

Originally posted by @martijnwalraven in #94 (comment)

@Renish-Development
Copy link
Author

Renish-Development commented Oct 5, 2020

How to use/implement Date type custom scalar in iOS?

I'm working with GraphQL. I have successfully implement Query and Mutation in iOS app.
I'm facing issue with Date Type. As per references, I have added --passthrough-custom-scalars in Run Script BUT I'm facing compilation issue.
I'm facing issue with "--passthrough-custom-scalars" variable. I have added below code in Run script but I'm facing an error.

DERIVED_DATA_CANDIDATE="${BUILD_ROOT}"
while ! [ -d "${DERIVED_DATA_CANDIDATE}/SourcePackages" ]; do
if [ "${DERIVED_DATA_CANDIDATE}" = / ]; then
echo >&2 "error: Unable to locate SourcePackages directory from BUILD_ROOT: '${BUILD_ROOT}'"
exit 1
fi
DERIVED_DATA_CANDIDATE="$(dirname "${DERIVED_DATA_CANDIDATE}")"
done
SCRIPT_PATH="${DERIVED_DATA_CANDIDATE}/SourcePackages/checkouts/apollo-ios/scripts"
if [ -z "${SCRIPT_PATH}" ]; then
echo >&2 "error: Couldn't find the CLI script in your checked out SPM packages; make sure to add the framework to your project."
exit 1
fi
cd "${SRCROOT}/${TARGET_NAME}"
"${SCRIPT_PATH}"/run-bundled-codegen.sh codegen:generate --target=swift --includes=./*/.graphql --localSchemaFile="schema.json" API.swift
"${SCRIPT_PATH}"/run-bundled-codegen.sh schema:download --endpoint="endpointURL"
#custom scalars
**$APOLLO_FRAMEWORK_PATH/check-and-run-apollo-codegen.sh generate $(find . -name '*.graphql') --schema schema.json --output API.swift --passthrough-custom-scalars**

Code for Define Date Scalars

public typealias DateTime = Date
extension DateTime: JSONDecodable, JSONEncodable {
  public init(jsonValue value: JSONValue) throws {
    guard let string = value as? String else {
      throw JSONDecodingError.couldNotConvert(value: value, to: String.self)
    }
    guard let date = ISO8601DateFormatter().date(from: string) else {
      throw JSONDecodingError.couldNotConvert(value: value, to: Date.self)
    }
    self = date
  }
  public var jsonValue: JSONValue {
    return ISO8601DateFormatter().string(from: self)
  }
}

I have reviewed schema JSON have a Date variable but API.swift still string type.
Here is my graphql mutation.

mutation AddPeriod($uid:ID!, $start_time : Date!, $flow: PeriodFlowInput){
  addPeriod(uid:$uid, start_time : $start_time, flow: $flow){
    id
    start_time
    end_time
    flow {
      value
      flow_time
    }
  }
}

How can I pass date in GraphQL mutation. Please help me. Thanks in advance

@designatednerd
Copy link
Contributor

designatednerd commented Oct 5, 2020

Hi @Renish-Development - I updated your comment to add some formatting.

It looks like your type is called Date rather than DateTime, which would mean I believe you need to have your typealias be:

typealias Date = Swift.Date

And then have your extension be on Date rather than DateTime. Give that a shot, let me know how it goes.

@Renish-Development
Copy link
Author

Renish-Development commented Oct 5, 2020

@designatednerd ,

1. Swift Date is Not available. I'm using Xcode 12. Please look at the attached screen shot.

2. I'm also facing issue with execute --passthrough-custom-scalars. Please let me know what I missed for custom scalars

$APOLLO_FRAMEWORK_PATH/check-and-run-apollo-codegen.sh generate $(find . -name '*.graphql') --schema schema.json --output API.swift --passthrough-custom-scalars

Screenshot 2020-10-06 at 12 52 09 AM

@designatednerd
Copy link
Contributor

Argh, I meant Foundation.Date. I always forget what's swift and what's foundation📈

And --output API.swift has to be the last argument - move --passthrough-custom-scalars before that.

@designatednerd designatednerd added the codegen Issues related to or arising from code generation label Oct 5, 2020
@designatednerd designatednerd changed the title @cerupcat @jzhw0130: You should be able to pass --passthrough-custom-scalars to apollo-codegen to avoid generating a typealias to String for custom scalars. You can then add your own alias and conversion code, see [here](https://github.com/apollographql/apollo-ios/issues/23#issuecomment-261697349) for an example. Using a custom Date scalar Oct 5, 2020
@Renish-Development
Copy link
Author

@designatednerd , Its' not working. Please can I do any mistake or issue due to the updated version of Xcode 12. I'm using Xcode 12. Please check with your side and let me know it's work or not. Happy top share more information

@designatednerd
Copy link
Contributor

I did notice that the param is --passthroughCustomScalars rather than --passthrough-custom-scalars - that could be part of it. If that's not what's not working, please let me know in more detail what isn't working and in what way it is not working. Thanks.

@designatednerd
Copy link
Contributor

Hi @Renish-Development is there anything more I can help with here or can we close this issue out?

@designatednerd
Copy link
Contributor

You're far from the only person who's asked a ton of questions about this so I've added a page to our example playground to try and clarify this. Please see #1474 for further details

@designatednerd designatednerd linked a pull request Oct 26, 2020 that will close this issue
@GlobeSyncTechnologies
Copy link

GlobeSyncTechnologies commented Oct 27, 2020

@designatednerd , Please add verify and step by step code or make tutorial as per latest version of GIT repo. Also I have not found proper documentation regarding custom scalar, pass token in API call, generate schema file with auth token and so on. I'm happy to help you anyway and if you can guide I will write tutorial for the same. Looking forward....

@designatednerd
Copy link
Contributor

@GlobeSyncTechnologies Please see the PR I linked for an example of using a custom scalar. Please check out our full tutorial for full setup instructions.

@IHNEL
Copy link

IHNEL commented Apr 9, 2021

Sorry for comment onto closed issue, but where can I set --passthroughCustomScalars for project configued with Swift Scripting?

ref https://www.apollographql.com/docs/ios/swift-scripting/

@designatednerd
Copy link
Contributor

Hi, normally it's better to open a new discussion to ask a new question. However since this is somewhat related, i'll answer it here: Please check out the initializer for ApolloCodegenOptions, where this and many other options can be set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codegen Issues related to or arising from code generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants